package jgl.vajra.management.manager;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;

import androidx.fragment.app.FragmentActivity;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.orhanobut.logger.Logger;

import java.io.IOException;

import jgl.vajra.management.bean.CarDetailsBean;
import jgl.vajra.management.bean.PictureBean;
import jgl.vajra.management.part.mine.activity.CarDetailsActivity;
import jgl.vajra.management.widget.BitmapImageNetActivity;
import jgl.vajra.management.widget.utils.BitmapUtils;
import jgl.vajra.management.widget.utils.UIUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ImageShowManager {

    private ImageShowManager() {

    }

    private static class ImageShowManagerSingleton {
        private static final ImageShowManager INSTANCE = new ImageShowManager();

    }

    public static ImageShowManager getInstance() {
        return ImageShowManager.ImageShowManagerSingleton.INSTANCE;
    }

    public void setPictureShow(FragmentActivity activity, ImageView imageView, String bucketName, String fileKey) {

        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder reqBuild = new Request.Builder();
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://steel.vin:29318/verify/private-image")
                .newBuilder();
        /* reqBuild.addHeader("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJwaG9uZSI6ImxOQ095aWpFNEgvUDArbzFza2lvbGc9PSIsImludml0YXRpb24iOiI1ODQwNTA1NzEyIiwidXNlclR5cGUiOiIyIiwianRpIjoiODg5ZTBkZjktY2UwOS00NTEzLTlhZjAtYTRkYWM2YjdlNTdjIiwiZXhwIjoxNjY2MjQ0MTQ4fQ.DkJ083vRn5eyHNAe0jZcn22OP3WIKeScdmsbhluMyg5TyEIzdjcn1A-Wgl3zBdP7pacwW1MT2nT_wvu2P3P6-T5lotXR-95rF_Lv_WtwbBUej__u2ra2UvDlWLp0cRjmXlD4N_eiqxlIl3MmXCszF3ZAOvtZPXdXj6o6loetFnEhAua_jQff5EHFvk7Yz8Fo0x1wbWVLnMcJPFMUxcHKwxhj2iPV58zOlVkwe2gXPRWVoXphNWxT4UQ4EoNfBqeqhrP9FDKk8tMG-O7gu8pbRPd8-Ki3TBjiz3aWQsPd_rVaEeUT4WpQtZDbRFtEUZZ3z-BbNz-CwCwLStgtIjG-nQ");*/
        reqBuild.addHeader("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        reqBuild.addHeader("bucketName", bucketName);
        reqBuild.addHeader("fileKey", fileKey);

        reqBuild.url(urlBuilder.build());
        Request request = reqBuild.build();
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Logger.d("onFailure", "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.body() != null) {
                    byte[] content = response.body().bytes();
                    activity.runOnUiThread(() -> {
                        Bitmap bmp = BitmapFactory.decodeByteArray(content, 0, content.length);
                        if (bmp != null) {
                            imageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, imageView.getWidth(),
                                    imageView.getHeight(), false));
                            Logger.e("response is ==", content.toString());
                        }
                    });

                }
            }
        });
    }


    public void setPictureShow(FragmentActivity activity, SubsamplingScaleImageView imageView, String bucketName, String fileKey) {

        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder reqBuild = new Request.Builder();
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://steel.vin:29318/verify/private-image")
                .newBuilder();
        /* reqBuild.addHeader("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJwaG9uZSI6ImxOQ095aWpFNEgvUDArbzFza2lvbGc9PSIsImludml0YXRpb24iOiI1ODQwNTA1NzEyIiwidXNlclR5cGUiOiIyIiwianRpIjoiODg5ZTBkZjktY2UwOS00NTEzLTlhZjAtYTRkYWM2YjdlNTdjIiwiZXhwIjoxNjY2MjQ0MTQ4fQ.DkJ083vRn5eyHNAe0jZcn22OP3WIKeScdmsbhluMyg5TyEIzdjcn1A-Wgl3zBdP7pacwW1MT2nT_wvu2P3P6-T5lotXR-95rF_Lv_WtwbBUej__u2ra2UvDlWLp0cRjmXlD4N_eiqxlIl3MmXCszF3ZAOvtZPXdXj6o6loetFnEhAua_jQff5EHFvk7Yz8Fo0x1wbWVLnMcJPFMUxcHKwxhj2iPV58zOlVkwe2gXPRWVoXphNWxT4UQ4EoNfBqeqhrP9FDKk8tMG-O7gu8pbRPd8-Ki3TBjiz3aWQsPd_rVaEeUT4WpQtZDbRFtEUZZ3z-BbNz-CwCwLStgtIjG-nQ");*/
        reqBuild.addHeader("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        reqBuild.addHeader("bucketName", bucketName);
        reqBuild.addHeader("fileKey", fileKey);

        reqBuild.url(urlBuilder.build());
        Request request = reqBuild.build();
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Logger.d("onFailure", "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.body() != null) {
                    byte[] content = response.body().bytes();
                    activity.runOnUiThread(() -> {
                        Bitmap decodedByte = BitmapFactory.decodeByteArray(content, 0, content.length);
                        float initImageScale = BitmapUtils.getInitImageScale(decodedByte, activity);
                        imageView.setMaxScale(initImageScale + 2.0f);//最大显示比例
                        imageView.setImage(ImageSource.bitmap(decodedByte), new ImageViewState(initImageScale, new PointF(0, 0), 0));
                        imageView.setVisibility(View.VISIBLE);
                    });

                }
            }
        });
    }
}
